using System;
using System.Drawing;
using System.Windows.Forms;
using Nuwanda.TfsSpotlight.Shell.Proxies.Interfaces;

namespace Nuwanda.TfsSpotlight.Shell.Proxies
{
    public class StatusBarProxy : IStatusBar
    {
        private readonly StatusStrip _statusBar;
        private readonly ControlInvoker _statusInvoker;

        private ToolStripStatusLabel MessageLabel
        {
            get { return this._statusBar.Items[0] as ToolStripStatusLabel; }
        }

        private ToolStripStatusLabel ProgressMessageLabel
        {
            get { return this._statusBar.Items[1] as ToolStripStatusLabel; }
        }

        private ToolStripProgressBar ProgressBar
        {
            get { return this._statusBar.Items[2] as ToolStripProgressBar; }
        }

        public StatusBarProxy(StatusStrip statusBar)
        {
            this._statusBar = statusBar;
            this._statusInvoker = new ControlInvoker(statusBar);

            ClearMessage();
            StopProgress();
        }

        public void SetMessage(string message, Image icon)
        {
            this._statusInvoker.RunInControlsThread(
                delegate
                    {
                        this.MessageLabel.Text = message;
                        this.MessageLabel.Image = icon;            
                    });
        }

        public void ClearMessage()
        {
            this._statusInvoker.RunInControlsThread(
                delegate
                    {
                        this.MessageLabel.Text = String.Empty;
                        this.MessageLabel.Image = null;
                    });
        }

        public void StartProgress(ProgressBarStyle style)
        {
            StartProgress(style, String.Empty);
        }

        public void StartProgress(ProgressBarStyle style, string message)
        {
            this._statusInvoker.RunInControlsThread(
                delegate
                    {
                        this.ProgressBar.Visible = true;
                        this.ProgressBar.Style = style;
                        this.ProgressMessageLabel.Text = message;
                        this.ProgressMessageLabel.Visible = String.IsNullOrEmpty(message) == false;
                    });
        }

        public void StopProgress()
        {
            this._statusInvoker.RunInControlsThread(
                delegate
                    {
                        this.ProgressBar.Visible = false;
                        this.ProgressMessageLabel.Visible = false;
                    });
        }

        public DialogResult ShowQuestion(string question, MessageBoxButtons buttons)
        {
            return this._statusInvoker.RunAndReturnInControlsThread(
                () => MessageBox.Show(question, Properties.Messages.CaptionQuestion, buttons, MessageBoxIcon.Question));
        }
    }
}